﻿using MasterEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccessLayer;
using System.Data;
using DataAccessLayer.DatabaseOperations;
using MasterEntities.Abstraction;
using Common.Helpers;

namespace BusinessLogicLayer.Implementations
{
    public class FizickoLiceBusinessLayerImplementation : AbstractBusinessRuleChecker<FizickoLice>
    {
        public override void CheckBusinessRuleToDelete(FizickoLice objToDelete)
        {
            if (objToDelete != null)
            {
                IGenericDataAccessLayer<FizickoLice> dal = new GenericDataAccessLayer<FizickoLice>();
                string where = string.Empty;
                if (GlobalVariables.Instance.dbConnection == Common.Helpers.Enums.DatabaseConnection.MasterApp1)
                    where = "fi.Id = ";
                else
                    where = "Id = ";

                FizickoLice dbFl = dal.GetEntity(fl => fl.Id == objToDelete.Id);

                if (dbFl != null)
                {
                    AbstractExecutionTemplate execution = new Delete();

                    execution.ExecuteOperation(objToDelete);

                    if (objToDelete.IdentifikacioniDokument != null)
                        execution.ExecuteOperation(objToDelete.IdentifikacioniDokument);                    
                }
            }
        }

        public override void CheckBusinessRuleToUpdate(FizickoLice objToUpdate)
        {
            if (objToUpdate != null)
            {
                IGenericDataAccessLayer<FizickoLice> dal = new GenericDataAccessLayer<FizickoLice>();
                string where = string.Empty;
                if (GlobalVariables.Instance.dbConnection == Common.Helpers.Enums.DatabaseConnection.MasterApp1)
                    where = "fi.Id = ";
                else
                    where = "Id = ";

                FizickoLice dbFl = dal.GetEntity(fl => fl.Id == objToUpdate.Id);
                if (dbFl != null)
                {
                    AbstractExecutionTemplate execution = new Update();
                    
                    if (objToUpdate.IdentifikacioniDokument != null)
                          execution.ExecuteOperation(objToUpdate.IdentifikacioniDokument);

                    if (objToUpdate.Otac != null)
                        objToUpdate.OtacId = objToUpdate.Otac.Id;

                    if (objToUpdate.Majka != null)
                        objToUpdate.MajkaId = objToUpdate.Majka.Id;

                    execution.ExecuteOperation(objToUpdate);
                }
            }
        }

        public override void CheckBusinessRuleToCreate(FizickoLice objToCreate)
        {
            if (objToCreate != null)
            {
                long identDokId = 0;
                AbstractExecutionTemplate execution = new Create();
                bool result;
                if (objToCreate.IdentifikacioniDokument != null)
                {
                    IDomainObject dom = objToCreate.IdentifikacioniDokument as IDomainObject;
                    identDokId = DataAccessLayer.Helper.EntityKeyGenerator.Instance.GenerateEntityKey(dom.SchemaName(), dom.GetTableName());
                    result = execution.ExecuteOperation(objToCreate.IdentifikacioniDokument);

                    if (identDokId != 0)
                        objToCreate.IdentifikacioniDokumentId = identDokId;
                }

                if (objToCreate.Otac != null)
                    objToCreate.OtacId = objToCreate.Otac.Id;

                if (objToCreate.Majka != null)
                    objToCreate.MajkaId = objToCreate.Majka.Id;

                result = execution.ExecuteOperation(objToCreate);
            }
        }
    }
}
